import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {
    constructor(props) {
        super(props);
        this.state = {
          mouse: false //标识鼠标移入、移出
        }
    }
    //鼠标移入、移出的回调
    handleMouse = (flag) => {
      return () => {
        this.setState({mouse: flag});
      }
    }
    // 勾选、取消勾选某一个todo的回调
    handleChange = (id) => {
      return (event) => {
        this.props.updateTodo(id, event.target.checked);
      }
    }
    handleDel = (id) => {
      return () => {
        this.props.delTodo(id);
      }
    }
  render() {
    const {id, name, done} = this.props;
    const {mouse} = this.state;
    return (
      <li style={{backgroundColor: mouse ? '#ddd': 'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave ={this.handleMouse(false)}>
        <label>
          <input type="checkbox" checked={done} onChange={this.handleChange(id)}/>
          <span>{name}</span>
        </label>
        <button className='btn btn-danger' style={{display:mouse? 'block' : 'none'}} onClick={this.handleDel(id)}>删除</button>
      </li>
    )
  }
}
